package stack;

import java.util.Stack;

// 有效括号
public class leetcode20 {
    public static void main(String[] args) {
        String s = "()[]{}";
        System.out.println(isValid(s));
    }

    public static boolean isValid(String s){
        if(s.length() <= 1) return false;
        Stack<Character> stack = new Stack<Character>();
        int i = 1;
        stack.push(s.charAt(0));
        while(i < s.length()){
            Character c = s.charAt(i);
            if(!stack.empty()) {
                Character pop = stack.pop();
                if((pop == '{' && c == '}')||(pop == '[' && c == ']')||(pop == '(' && c == ')')){
                    i++;
                }
                else{
                    stack.push(pop);
                    stack.push(c);
                    i++;
                }
            }else{
                stack.push(c);
                i++;
            }
        }
        if(stack.empty()) return true;
        return false;
    }
}
